package com.sx.micowell.module.homePage

import android.content.res.ColorStateList
import android.view.Gravity
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.dou361.ijkplayer.widget.PlayStateParams
import com.dou361.ijkplayer.widget.PlayerView
import com.example.mvvm.base.data.DataBindingConfig
import com.google.android.material.tabs.TabLayout
import com.gyf.immersionbar.ImmersionBar
import com.miloyu.modelnet.response.getUrl
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragHomeBinding

import com.sx.micowell.framework.app.MyApplication
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath.Companion.HOME
import com.sx.micowell.framework.model.HomeModel

import com.sx.micowell.module.MainActivity
import com.sx.widget.app.DisplayUtil
import kotlinx.android.synthetic.main.frag_home.*


/**
 * @Description 主页-首页
 * @Author 梁浩文
 * @Date 2021/7/27 9:15
 */

@Route(path = HOME)
class HomeFragment : MyFragment<FragHomeBinding, MainActivity>() {
    private var player: PlayerView? = null
    lateinit var viewModel: HomeModel
    override fun initViewModel() {
        viewModel = getActivityScopeViewModel(HomeModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig =
        DataBindingConfig(R.layout.frag_home, BR.viewModel, viewModel)


    override fun initView() {
        ImmersionBar.with(this).init()
        home_frag.post {
            home_frag.layoutParams.height=DisplayUtil.getWindowsHeight(context)
        }
        childFragmentManager.beginTransaction().add(
            R.id.home_frag,
            RouteManager.getInstance().getRecommendCourseFragment()
        ).commit()
        binding.refreshLayout.setOnRefreshListener {
            viewModel.refreshLiveData.postValue(true)
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.tabData.observe(this){
            initTab(it)
        }
        viewModel.homeVideoData.observe(this){
            startPlay(
                it.info.url.getUrl(),
                it.info.cover.getUrl()
            )
        }
        viewModel.refreshLiveData.observe(this){
            viewModel.reqHomeVideo()
        }
    }

    override fun initData() {
        initPlayer()
        viewModel.initTab()
        viewModel.reqHomeVideo()
//        power_line.test()
//        calories_line.test()
//        month_chart.test()
    }



    /**初始化播放视频*/
    fun initPlayer(){
        player = PlayerView(activity)
            .setScaleType(PlayStateParams.fitparent)
            .forbidTouch(false)
            .hideHideTopBar(true)
            .hideSteam(true)
            .hideFullscreen(true)
            .hideRotation(true)
    }

    /**开始播放视频*/
    fun startPlay(mp4: String, bg: String){
        context?.let {
            player!!.showThumbnail { ivThumbnail ->
                Glide.with(it)
                    .load(bg)
                    .placeholder(R.color.black)
                    .error(R.color.black)
                    .into(ivThumbnail)
            } .setPlaySource(
                MyApplication.instance().getProxy(it)
                    ?.getProxyUrl("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
            )
                .startPlay()
        }

    }

    /**初始化Tab*/
    fun initTab(list: List<String>){
        //文字颜色选择器
        val arrayState = arrayOf(
            intArrayOf(
                android.R.attr.state_selected,
                android.R.attr.state_enabled
            ), intArrayOf(android.R.attr.state_enabled)
        )
        val arrayColor = intArrayOf(
            ContextCompat.getColor(
                context!!, R.color.c_333333
            ),
            ContextCompat.getColor(
                context!!, R.color.c_999999
            )
        )
        var selTv= ColorStateList(arrayState, arrayColor)
        list.forEach {
            var tv=TextView(context)
            tv.text=it
            tv.gravity= Gravity.CENTER
            tv.setTextColor(selTv)
            var isFirst=list.indexOf(it)==0
            tv.textSize=if(isFirst)24f else 16f
            tv.paint.isFakeBoldText=isFirst
            home_tab.addTab(home_tab.newTab().setCustomView(tv))
        }
        //监听器
        home_tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                (tab!!.view.tab!!.customView as TextView).textSize = 24f
                (tab!!.view.tab!!.customView as TextView).paint.isFakeBoldText = true
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                (tab!!.view.tab!!.customView as TextView).textSize = 16f
                (tab!!.view.tab!!.customView as TextView).paint.isFakeBoldText = false
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
    }


}